package com.example.da40;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.da20.TestConfig;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.example.Easy2LearnApplication;
import org.example.domain.Manual40;
import org.example.mapper.Manual40Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest(classes = {Easy2LearnApplication.class, TestConfig.class})
public class getPdfText {

    @Test
    public void test(){

    }

    @Resource
    private Manual40Mapper manual40Mapper;

    @SneakyThrows
    @Test
    public void test1() {
        String path = "C:\\Users\\SIM\\Desktop\\40.txt";
        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line;
            StringBuilder context = new StringBuilder();
            while ((line = br.readLine()) != null) {
                context.append(line);
                if (line.equals("------------------------")) {
                    System.out.println(context);
                    Manual40 manual40 = getContext(context);
                    if (manual40!=null) {
                        System.out.println(manual40);
                        manual40.setAirplane("DA40");
                        manual40Mapper.insert(manual40);
                    }
                    context = new StringBuilder();
                }
            }
        }
    }

    @Test
    public void test2(){
        List<Manual40> manual40s = manual40Mapper.selectList(new LambdaQueryWrapper<Manual40>());
        for (Manual40 manual40 : manual40s) {
            String index = manual40.getIndex();
            String context = manual40.getContext();
            System.out.println("手册章节号："+index);
            System.out.println("内容："+context);
            int i = context.indexOf("页内容：");
            String substring = context.substring(i + 4, context.length() - 1);
            System.out.println(substring);
            manual40.setContext(substring);
//            manual40Mapper.updateById(manual40);
        }
    }

    public  Manual40 getContext(StringBuilder context){
        String contextStr = context.toString();

        // 匹配ATA后跟空格和两位数字的模式
        Pattern pattern = Pattern.compile("ATA\\s(\\d{2})");
        Matcher matcher = pattern.matcher(contextStr);
        
        // 输出所有匹配结果
        while(matcher.find()) {
            Manual40 manual40 = new Manual40();
            String index = matcher.group();
            System.out.println("找到ATA编号: " + index);
            manual40.setIndex(index);
            manual40.setContext(contextStr);
            return manual40;
        }
        return null;
    }

}
